package com.bs.sc.mgr.goods.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.front.goodsCar.dao.GoodsCarDao;
import com.bs.sc.front.user.entity.User;
import com.bs.sc.mgr.goods.dao.GoodsDao;
import com.bs.sc.mgr.goods.entity.Goods;
import com.bs.sc.mgr.goods.service.GoodsService;
import com.bs.sc.mgr.order.dao.OrderDao;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private GoodsCarDao goodsCarDao;
	@Autowired
	private OrderDao orderDao;

	@Override
	public Page<Goods> findGoodsList(Pageable pageable) {
		return goodsDao.findAll(pageable);
	}

	@Override
	public void addOrUpdateGoods(Goods goods) {
		goodsDao.save(goods);
	}

	@Override
	public void deleteGoods(Goods goods) {
		goodsDao.delete(goods);
	}

	/**
	 * 删除商品图片
	 */
	@Override
	public boolean deleteGoodsPic(Goods goods) {
		System.out.println("要删除的商品：" + goods);
		boolean b = false;
		try {
			String imgUrl = goods.getImgUrl();
			if (StringUtils.isEmpty(imgUrl) || !imgUrl.contains("upload/")) {
				return true;
			}
			String fileName = imgUrl.substring(imgUrl.indexOf("upload/"));
			String path = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\" + fileName;
			File f = new File(path);
			if (f.exists()) {
				f.delete();
			}
			b = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}

	@Override
	public Goods getGoods(String id) {
		System.out.println("id:" + id);
		Optional<Goods> optional = goodsDao.findById(id);
		if (optional.isPresent()) {
			return optional.get();
		}
		return null;
	}

	@Override
	public List<Goods> findBoutiqueGoods() {
		return goodsDao.findByBoutique("Y");
	}

	@Override
	public List<Goods> findAllGoods() {
		List<Goods> goodsList = goodsDao.findAll();
		return goodsList;
	}

	@Override
	public AjaxJson getOrderGoods(HttpServletRequest request, String ids) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取订单商品失败");
		try {
			if (StringUtils.isEmpty(ids)) {
				j.setMsg("数据异常");
				return j;
			}
			String[] idList = ids.split(",");
			User user = (User) request.getSession().getAttribute("user");
			List<Map<String, Object>> carList = goodsCarDao.findOrderGoods(user.getId(), idList);
			List<Map<String, Object>> orderInfos = new ArrayList<Map<String, Object>>();
			if (carList.size() <= 0) {
				Map<String, Object> temp = new HashMap<String, Object>();
				Optional<Goods> o = goodsDao.findById(idList[0]);
				temp.put("count", 1);
				temp.put("goods", o.get());
				orderInfos.add(temp);
			}
			for (Map<String, Object> m : carList) {
				Map<String, Object> temp = new HashMap<String, Object>();
				temp.put("count", m.get("count"));
				Optional<Goods> goods = goodsDao.findById((String) m.get("goodsId"));
				temp.put("goods", goods.get());
				orderInfos.add(temp);
			}
			j.setCode("success");
			j.setMsg("获取订单商品成功");
			j.setData(orderInfos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public List<Goods> findByClassify(String classify) {
		List<Goods> goodsList = goodsDao.findByClassify(Integer.valueOf(classify));
		return goodsList;
	}

	@Override
	public List<Map<String, String>> findCommentByGoodsId(String goodsId) {
		List<Map<String, String>> list = orderDao.findCommentByGoodsId(goodsId);
		return list;
	}
}
